<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestEvent extends PHPUnit_Framework_TestCase {
	function test_prepare() {
		$expected_region_keys = array('region1', 'region2', 'region3');
		$expected_region_values = array('value1', 'value2', 'value3');
		$expected_location_field = 'location field';
		$expected_archive_field = 'archive field';
		$expected_new = false;
		$expected_date_start_day = 'start day';
		$expected_date_start_month = 'start month';
		$expected_date_start_year = 'start year';
		$expected_post_item_id = false;
		$expected_multiple_days = false;
		$expected_post_multiple_days = false;
		$expected_link = 'link';
		$expected_desqlized_link = 'desqlized link';
		$expected_description = 'description';
		$expected_desqlized_description = 'desqlized description';
		$expected_link_field = 'link field';
		$expected_description_field = 'description field';
		$expected_multiple_days_field = 'multiple days field';
		$expected_date_field = 'date field';

		$expected_result = array(
				$expected_location_field, 
				$expected_archive_field, 
				$expected_date_field, 
				$expected_multiple_days_field, 
				$expected_link_field,
				$expected_description_field);

		$expected_item = array(
				'date_start_day' => $expected_date_start_day,
				'date_start_month' => $expected_date_start_month,
				'date_start_year' => $expected_date_start_year,
				'multiple_days' => $expected_multiple_days,
				'link' => $expected_link,
				'description' => $expected_description);

		$expected_regions = array('keys' => $expected_region_keys, 'values' => $expected_region_values);

		$page = new Page();
		
		$event = new Event($page);	

		$mock_region_manager = $this->getMock('RegionManager');
		$mock_region_manager->expects($this->once())
				->method('region_get_regions_kv')
				->with()
				->will($this->returnValue($expected_regions));
		$event->set_region_manager($mock_region_manager);
		$mock_creator = $this->getMock('FormCreator');
		$mock_creator->expects($this->at(0))
				->method('input_select')
				->with("Regio", "region", array_merge(array("NEW"), $expected_region_keys))
				->will($this->returnValue($expected_location_field));
		$mock_creator->expects($this->at(1))
				->method('input_checkbox')
				->with("Een nieuwe editie (datum) invoeren en de vorige archiveren", "archive")
				->will($this->returnValue($expected_archive_field));
		$mock_creator->expects($this->at(2))
				->method('input_date')
				->with("Begindatum", 
						"date_start", 
						array(
								'day' => $expected_date_start_day, 
								'month' => $expected_date_start_month,
								'year' => $expected_date_start_year))
				->will($this->returnValue($expected_date_field));
		$mock_creator->expects($this->at(3))
				->method('input_checkbox')
				->with("Meerdaags evenement", 
						"multiple_days", 
						($expected_post_item_id && $expected_multiple_days) || $expected_post_multiple_days, 
						"document.itemform.submit()") //TODO: remove javascript
				->will($this->returnValue($expected_multiple_days_field));
		$mock_creator->expects($this->at(4))
				->method('input_field')
				->with("Link", "link", $expected_desqlized_link)
				->will($this->returnValue($expected_link_field));
		$mock_creator->expects($this->at(5))
				->method('input_text')
				->with("Beschrijving", "description", $expected_desqlized_description)
				->will($this->returnValue($expected_description_field));
		$event->set_form_creator($mock_creator);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->at(0))
				->method('db_desqlize')
				->with($expected_link)
				->will($this->returnValue($expected_desqlized_link));
		$mock_db_access->expects($this->at(1))
				->method('db_desqlize')
				->with($expected_description)
				->will($this->returnValue($expected_desqlized_description));
		$event->set_db_accessor($mock_db_access);

		$actual = $event->prepare($expected_new, $expected_item, $expected_inputs);
		$this->assertEquals($expected_result, $actual);
	}

	function test_prepare_with_new_region() {
		$expected_region_keys = array('region1', 'region2', 'region3');
		$expected_region_values = array('value1', 'value2', 'value3');
		$expected_location_field = 'location field';
		$expected_archive_field = 'archive field';
		$expected_new = false;
		$expected_date_start_day = 'start day';
		$expected_date_start_month = 'start month';
		$expected_date_start_year = 'start year';
		$expected_post_item_id = false;
		$expected_multiple_days = false;
		$expected_post_multiple_days = false;
		$expected_link = 'link';
		$expected_desqlized_link = 'desqlized link';
		$expected_description = 'description';
		$expected_desqlized_description = 'desqlized description';
		$expected_link_field = 'link field';
		$expected_description_field = 'description field';
		$expected_multiple_days_field = 'multiple days field';
		$expected_date_field = 'date field';
		$expected_region_field = 'region field';
		$expected_region_id = 321;

		$expected_result = array(
				$expected_region_field,
				$expected_location_field, 
				$expected_archive_field, 
				$expected_date_field, 
				$expected_multiple_days_field, 
				$expected_link_field,
				$expected_description_field);

		$expected_item = array(
				'date_start_day' => $expected_date_start_day,
				'date_start_month' => $expected_date_start_month,
				'date_start_year' => $expected_date_start_year,
				'multiple_days' => $expected_multiple_days,
				'link' => $expected_link,
				'description' => $expected_description,
				'region_id' => $expected_region_id);

		$expected_regions = array('keys' => $expected_region_keys, 'values' => $expected_region_values);

		$_POST['region'] = 'NEW';

		$page = new Page();
		
		$event = new Event($page);	

		$mock_region_manager = $this->getMock('RegionManager');
		$mock_region_manager->expects($this->once())
				->method('region_get_regions_kv')
				->with(1)
				->will($this->returnValue($expected_regions));
		$event->set_region_manager($mock_region_manager);

		$mock_creator = $this->getMock('FormCreator');
		$mock_creator->expects($this->at(0))
				->method('input_field')
				->with('Regio', 'region_name')
				->will($this->returnValue($expected_region_field));
		$mock_creator->expects($this->at(1))
				->method('input_select')
				->with("Locatie", 
						"region", 
						$expected_region_keys, 
						$expected_region_values, 
						"kies een regio", 
						$expected_region_id)
				->will($this->returnValue($expected_location_field));
		$mock_creator->expects($this->at(2))
				->method('input_checkbox')
				->with("Een nieuwe editie (datum) invoeren en de vorige archiveren", "archive")
				->will($this->returnValue($expected_archive_field));
		$mock_creator->expects($this->at(3))
				->method('input_date')
				->with("Begindatum", 
						"date_start", 
						array(
								'day' => $expected_date_start_day, 
								'month' => $expected_date_start_month,
								'year' => $expected_date_start_year))
				->will($this->returnValue($expected_date_field));
		$mock_creator->expects($this->at(4))
				->method('input_checkbox')
				->with("Meerdaags evenement", 
						"multiple_days", 
						($expected_post_item_id && $expected_multiple_days) || $expected_post_multiple_days, 
						"document.itemform.submit()") //TODO: remove javascript
				->will($this->returnValue($expected_multiple_days_field));
		$mock_creator->expects($this->at(5))
				->method('input_field')
				->with("Link", "link", $expected_desqlized_link)
				->will($this->returnValue($expected_link_field));
		$mock_creator->expects($this->at(6))
				->method('input_text')
				->with("Beschrijving", "description", $expected_desqlized_description)
				->will($this->returnValue($expected_description_field));
		$event->set_form_creator($mock_creator);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->at(0))
				->method('db_desqlize')
				->with($expected_link)
				->will($this->returnValue($expected_desqlized_link));
		$mock_db_access->expects($this->at(1))
				->method('db_desqlize')
				->with($expected_description)
				->will($this->returnValue($expected_desqlized_description));
		$event->set_db_accessor($mock_db_access);

		$actual = $event->prepare($expected_new, $expected_item, $expected_inputs);
		$this->assertEquals($expected_result, $actual);
	}

	function test_get_edit_everything() {
		$expected_id = 321;
		$expected_sql = 'sql';
		$expected_sql_result = 'sql result';
		$expected_result = 'result';

		$page = new Page();
		$event = new Event($page, $expected_id);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(array(array('*')), array("EVENT"), "item_id = $expected_id")
				->will($this->returnValue($expected_sql));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->once())
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->returnValue($expected_result));
		$event->set_db_accessor($mock_db_access);

		$actual = $event->get_edit_everything();

		$this->assertEquals($expected_result, $actual);
	}

	function test_check_input() {
		$expected_description = 'description';
		$expected_region_name = 'region name';
		$expected_region = 5;
		$expected_link = null;
		$expected_start_day = '07';
		$expected_start_month = '010';
		$expected_start_year = '011';
		$expected_end_day = null;

		$expected_values = array(
				'description' => $expected_description,
				'region_name' => $expected_region_name,
				'region' => $expected_region,
				'link' => $expected_link,
				'date_start_day' => $expected_start_day,
				'date_start_month' => $expected_start_month,
				'date_start_year' => $expected_start_year,
				'date_end_day' => $expected_end_day);

		$page = new Page();	
		$event = new Event($page);

		$mock_checker = $this->getMock('InputChecker');
		$mock_checker->expects($this->at(0))
				->method('check_tags_advanced')
				->with($expected_description, "beschrijving")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(1))
				->method('check_tag_closures')
				->with($expected_description)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(2))
				->method('check_exists')
				->with($expected_description, "beschrijving")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(3))
				->method('check_input')
				->with(true, "regio niet aangegeven")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(4))
				->method('check_date_valid')
				->with($expected_start_day, $expected_start_month, $expected_start_year, "datum")
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(5))
				->method('check_date_future')
				->with($expected_start_day, $expected_start_month, $expected_start_year, "datum")
				->will($this->returnValue(true));

		$event->set_input_checker($mock_checker);
		$actual = $event->check_input($expected_values, false);
		$this->assertTrue($actual);
	}
}


?>
